#include "BBGraphics2D.h"


BBGraphics2D* BBGraphics2D::createGraphics2D( const char* pathFile)
{
	BBGraphics2D* graphic2D = new BBGraphics2D();
	if(graphic2D)
	{
		graphic2D->setGraphics2D(graphic2D->addImage(pathFile));
		return graphic2D;
	}
	BB_SAFE_DELETE(graphic2D);
	return NULL;
}

BBGraphics2D::BBGraphics2D( void )
	:m_pGraphics(NULL)
{
}

BBGraphics2D::~BBGraphics2D(void)
{
}


#if (BB_TARGET_RENDEENQINE == BB_RENDEENQINE_COCOS2DX)
BBGraphics2D* BBGraphics2D::createGraphics2D( CCTexture2D* texture )
{
	BBGraphics2D* graphic2D = new BBGraphics2D();
	if(graphic2D && texture != NULL)
	{
		graphic2D->setGraphics2D(texture);
		return graphic2D;
	}
	BB_SAFE_DELETE(graphic2D);
	return NULL;
}

CCTexture2D* BBGraphics2D::addImage( const char* filePath )
{
	CCTexture2D * texture = NULL;
	CCImage* pImage = NULL;
	std::string pathKey = filePath;
	pathKey = CCFileUtils::sharedFileUtils()->fullPathForFilename(pathKey.c_str());
	if (pathKey.size() == 0)
		return NULL;
	std::string lowerCase(pathKey);
	for (unsigned int i = 0; i < lowerCase.length(); ++i)
	{
		lowerCase[i] = tolower(lowerCase[i]);
	}

	if(std::string::npos != lowerCase.find(".pvr"))
	{
		texture = addPVRImage(pathKey.c_str());
	}
	else
	{
		CCImage::EImageFormat eImageFormat = CCImage::kFmtUnKnown;
		if (std::string::npos != lowerCase.find(".png"))
			eImageFormat = CCImage::kFmtPng;
		else if (std::string::npos != lowerCase.find(".jpg") || std::string::npos != lowerCase.find(".jpeg"))
			eImageFormat = CCImage::kFmtJpg;
		else if (std::string::npos != lowerCase.find(".tif") || std::string::npos != lowerCase.find(".tiff"))
			eImageFormat = CCImage::kFmtTiff;
		else if (std::string::npos != lowerCase.find(".webp"))
			eImageFormat = CCImage::kFmtWebp;
		pImage = new CCImage();
		if(pImage == NULL)
			return NULL;

		unsigned long nSize = 0;
		unsigned char* pBuffer = CCFileUtils::sharedFileUtils()->getFileData(pathKey.c_str(), "rb", &nSize);

		bool bRet = pImage->initWithImageData((void*)pBuffer, nSize, eImageFormat);
		CC_SAFE_DELETE_ARRAY(pBuffer);
		if(!bRet)
			return NULL;

		texture = new CCTexture2D();
		if( texture && texture->initWithImage(pImage) )
			return texture;
		else
			CCLOG("Couldn't create texture for file:%s in CCTextureCache", filePath);
	}
	CC_SAFE_RELEASE(pImage);
	return NULL;
}


CCTexture2D* BBGraphics2D::addPVRImage( const char* pvrFile )
{
	CCTexture2D* texture = NULL;
	std::string key(pvrFile);
	std::string fullpath = CCFileUtils::sharedFileUtils()->fullPathForFilename(key.c_str());
	texture = new CCTexture2D();
	if(texture != NULL && texture->initWithPVRFile(fullpath.c_str()) )
	{
		texture->autorelease();
	}
	else
	{
		CCLOG("cocos2d: Couldn't add PVRImage:%s in CCTextureCache",key.c_str());
		CC_SAFE_DELETE(texture);
	}
	return texture;
}

CCTexture2D* BBGraphics2D::getGraphics2D()
{
	return m_pGraphics;
}

void BBGraphics2D::setGraphics2D(CCTexture2D* var)
{
	m_pGraphics = var;
}

#endif




